package com.sammy.appmonitor.receivers;

import java.util.Timer;
import java.util.TimerTask;

import com.sammy.appmonitor.communication.ParseClient;
import com.sammy.appmonitor.main.MainActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import com.sammy.utils.MyLog;

public class ConnectivityManagerReceiver extends BroadcastReceiver {
	
	private static final String TAG = "MonitorApplicationsReceiver.";
	private Context _context;
	@Override
	public void onReceive(Context context, Intent intent) {
		MyLog.d(MainActivity.PROCESS_TAG,TAG+"onReceive");
		_context = context;
	     boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
	     if(noConnectivity){
	    	 //start task after 10 seconds to allow internet connection to recover
	    	 TimerTask myTask = new TimerTask(){
	    		 public void run(){
	    			 ParseClient.getInstance().consumeQueue(_context);
	    		 }
	    	 };
	         Timer myTimer = new Timer();	  
	         myTimer.schedule(myTask, 10000); 
	     }
	     else{
	    	 ParseClient.getInstance().consumeQueue(context); 
	     }						
	}

}
